<?php
/**
 * Descript
 * User: blue
 * Date: 2018/8/16 10:01
 */
namespace app\lib\serverLib;

use PHPMailer\PHPMailer\PHPMailer;

class Email{
    private $phpMailer;
    public function __construct()
    {
        if(!$this->phpMailer instanceof PHPMailer){
            $this->phpMailer=new PHPMailer();
        }
    }

    /**
     * 发送邮件
     * @param $toMail 接收邮箱地址
     * @param $content 邮件内容
     * @throws \PHPMailer\PHPMailer\Exception
     */
    public function sendEmail($toMail,$content){
        try {
            //Server settings
            // $this->phpMailer->SMTPDebug = 2;                                 // Enable verbose debug output
            $this->phpMailer->CharSet="utf-8";
            $this->phpMailer->isSMTP();                                      // Set mailer to use SMTP
            $this->phpMailer->Host = config('send.send_mail_host');  // Specify main and backup SMTP servers
            $this->phpMailer->SMTPAuth = true;                               // Enable SMTP authentication
            $this->phpMailer->Username = config('send.send_username');                 // SMTP username
            $this->phpMailer->Password = config('send.send_secret');                           // SMTP password
            $this->phpMailer->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $this->phpMailer->Port = config('send.email_port');                                    // 465,994

            //Recipients
            $this->phpMailer->setFrom(config('send.send_username'), config('send.send_title'));
            // $this->phpMailer->addAddress('joe@example.net', 'Joe User');     // Add a recipient
            $this->phpMailer->addAddress($toMail);               // Name is optional
            // $this->phpMailer->addReplyTo('info@example.com', 'Information');
            // $this->phpMailer->addCC('cc@example.com');
            // $this->phpMailer->addBCC('bcc@example.com');

            //Attachments
            // $this->phpMailer->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            // $this->phpMailer->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $this->phpMailer->isHTML(true);                                  // Set email format to HTML
            $this->phpMailer->Subject = 'Rabbit注册验证码';
            $this->phpMailer->Body    = "注册验证码是:".$content;
            // $this->phpMailer->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $res=$this->phpMailer->send();
            echo 'Message has been sent';
            return $res;
        } catch (Exception $e) {
            echo 'Message could not be sent. Mailer Error: ', $this->phpMailer->ErrorInfo;
        }
    }

}